/******************************************************************************
* File description: SettingDefaultGuide.qml
* Function: 设置-默认设置：默认指导建议
* Current Version: 1.0.0
* Author: 张宇轩
* Date: 2024-08-30
*  -----
*  Copyright by 博坤生物 .
 *****************************************************************************/
import QtQuick 2.14
import QtQuick.Controls 2.14
import QtQuick.Layouts 1.14
import Qt.labs.qmlmodels 1.0

Item {
    id: root

    /* property 定义开始 */
    property var tableModel: []

    // 私有属性
    property var _horHeaderData: ["信息", "操作"]
    property int _horHeaderHeight: 40
    property int _tableItemHeight: 50
    property var _tableModel: root.tableModel
    /* property 定义结束 */

    ColumnLayout {
        anchors.fill: parent
        spacing: 18

        // 下拉框
        LabelComboBox {
            Layout.preferredWidth: 260
            Layout.preferredHeight: 40
            comboBoxWidth: width
            comboBoxHeight: height
            showListMaxCountinPage: 3
            model: ["基因分析仪", "试剂盒类型", "试剂盒名称", "体系(μl)"]
            comboBoxText: ""
        }

        // 表格
        Item {
            id: tableContainer
            Layout.fillWidth: true
            Layout.fillHeight: true

            // 表头部分
            // 横向表头
            Item {
                id: horizontalHeader
                width: parent.width
                height: _horHeaderHeight
                anchors {
                    top: parent.top
                }

                Row {
                    id: horHeaderRowLayout
                    spacing: 0

                    Repeater {
                        id: horHeaderItemRepeater
                        model: _horHeaderData

                        Item {
                            id: horheaderItem
                            width: horizontalHeader.width / _horHeaderData.length
                            height: horizontalHeader.height

                            Rectangle {
                                id: horHeaderBg
                                anchors.fill: parent
                                border.width: 0
                                color: "#FFEBF3FF"

                                Text {
                                    id: horHeaderText
                                    text: qsTr(modelData)
                                    anchors.centerIn: parent
                                    font.pixelSize: 16
                                    font.family: "Alibaba PuHuiTi"
                                    color: "#FF222222"
                                    leftPadding: 15
                                    rightPadding: 15
                                }
                            }

                            // 画边框
                            Canvas {
                                id: horHeaderCanvas
                                antialiasing: true
                                anchors.fill: parent
                                onPaint: {
                                    var ctx = getContext("2d")
                                    ctx.clearRect(0, 0, width, height)
                                    ctx.strokeStyle = "#FFCCCCCC"
                                    ctx.lineWidth = 2
                                    ctx.beginPath()

                                    // 左边框
                                    ctx.moveTo(0, 0)
                                    ctx.lineTo(0, height)
                                    ctx.stroke()

                                    // 上边框
                                    ctx.moveTo(0, 0)
                                    ctx.lineTo(width, 0)
                                    ctx.stroke()

                                    // 下边框
                                    ctx.moveTo(0, height)
                                    ctx.lineTo(width, height)
                                    ctx.stroke()
                                }
                            }

                        }
                    }
                }
            }

            // 表格部分
            TableView {
                id: tableContent
                height: parent.height - horizontalHeader.height
                anchors {
                    top: parent.top
                    topMargin: _horHeaderHeight
                    left: parent.left
                    right: parent.right
                }
                clip: true
                interactive: false // 不可触摸滑动
                boundsBehavior: Flickable.StopAtBounds
                columnSpacing: 0
                rowSpacing: 0

                Component.onCompleted:  {
                    root.tableModel = [
                                {"信息": "789", "操作": ""},
                                {"信息": "789", "操作": ""},
                                {"信息": "789", "操作": ""}
                            ]
                }

                columnWidthProvider: function(column){
                    return tableContent.width / _horHeaderData.length
                }

                rowHeightProvider: function(row){
                    return _tableItemHeight
                }

                model: TableModel {
                    id: tableModelCon

                    TableModelColumn {display: "信息"}
                    TableModelColumn {display: "操作"}

                    rows: _tableModel
                }

                // 使用 DelegateChooser 对表格的不同项进行代理
                delegate: DelegateChooser {
                    // 信息
                    DelegateChoice {
                        column: 0
                        delegate: Item {
                            id: infoDelegate
                            implicitWidth: tableContent.columnWidthProvider(column)
                            implicitHeight: tableContent.rowHeightProvider(row)

                            Label {
                                id: infoText
                                anchors {
                                    horizontalCenter: parent.horizontalCenter
                                    verticalCenter: parent.verticalCenter
                                }

                                font {
                                    family: "Alibaba PuHuiTi"
                                    pixelSize: 15
                                }
                                color: "#FF222222"
                                text: display
                            }

                            // 边框
                            Canvas {
                                anchors.fill: parent
                                antialiasing: true
                                onPaint: {
                                    var ctx = getContext("2d")
                                    ctx.strokeStyle = "#FFCCCCCC"
                                    ctx.lineWidth = 2
                                    // 左边框
                                    ctx.moveTo(0, 0)
                                    ctx.lineTo(0, height)
                                    ctx.stroke()
                                    // 下边框
                                    ctx.moveTo(0, height)
                                    ctx.lineTo(width, height)
                                    ctx.stroke()
                                }
                            }

                        }
                    }

                    // 操作
                    DelegateChoice {
                        column: 1
                        delegate: Item {
                            id: opDelegate
                            implicitWidth: tableContent.columnWidthProvider(column)
                            implicitHeight: tableContent.rowHeightProvider(row)

                            RowLayout {
                                id: opRowLayout
                                anchors {
                                    fill: parent
                                    leftMargin: 35
                                    rightMargin: 35
                                }
                                spacing: 20

                                CustomHorSpacerItem {}

                                // 添加
                                PureButton {
                                    id: addBtn
                                    Layout.preferredWidth: 120
                                    Layout.preferredHeight: 36
                                    Layout.alignment: Qt.AlignVCenter
                                    pureBtnWidth: width
                                    pureBtnHeight: height
                                    pureBtnRadius: 2
                                    pureHoveredColor: "#FFADD8E6"
                                    pureHoveredBorderColor: "#999"
                                    pureFontHoveredColor: "#222"
                                    pureText: "添加"
                                }

                                // 删除
                                PureButton {
                                    id: delBtn
                                    Layout.preferredWidth: 120
                                    Layout.preferredHeight: 36
                                    Layout.alignment: Qt.AlignVCenter
                                    pureBtnWidth: width
                                    pureBtnHeight: height
                                    pureBtnRadius: 2
                                    pureHoveredColor: "#FFADD8E6"
                                    pureHoveredBorderColor: "#999"
                                    pureFontHoveredColor: "#222"
                                    pureText: "删除"
                                }

                                // 默认
                                PureButton {
                                    id: defaultBtn
                                    Layout.preferredWidth: 120
                                    Layout.preferredHeight: 36
                                    Layout.alignment: Qt.AlignVCenter
                                    pureBtnWidth: width
                                    pureBtnHeight: height
                                    pureBtnRadius: 2
                                    pureHoveredColor: "#FFADD8E6"
                                    pureHoveredBorderColor: "#999"
                                    pureFontHoveredColor: "#222"
                                    pureText: "默认"
                                }

                                CustomHorSpacerItem {}
                            }

                            // 边框
                            Canvas {
                                anchors.fill: parent
                                antialiasing: true
                                onPaint: {
                                    var ctx = getContext("2d")
                                    ctx.strokeStyle = "#FFCCCCCC"
                                    ctx.lineWidth = 2
                                    // 左边框
                                    ctx.moveTo(0, 0)
                                    ctx.lineTo(0, height)
                                    ctx.stroke()
                                    // 下边框
                                    ctx.moveTo(0, height)
                                    ctx.lineTo(width, height)
                                    ctx.stroke()
                                }
                            }
                        }
                    }
                }

                onWidthChanged: {
                    tableContent.forceLayout()
                }

                onHeightChanged: {
                    tableContent.forceLayout()
                }
            }

            // 右边框
            Canvas {
                id: containerCanvas
                anchors.fill: parent
                antialiasing: true
                onPaint: {
                    var ctx = getContext("2d")
                    ctx.clearRect(0, 0, width, height)
                    ctx.strokeStyle = "#FFCCCCCC"
                    ctx.lineWidth = 2
                    ctx.beginPath()
                    ctx.moveTo(width, 0)
                    ctx.lineTo(width, tableContent.rows * _tableItemHeight + _horHeaderHeight)
                    ctx.stroke()
                }
            }
        }
    }
}
